package com.ztesoft.web.common.controller;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ztesoft.framework.util.FrameWorkConstants;
import com.ztesoft.framework.util.StringUtils;
import com.ztesoft.iocp.common.Configuration;
import com.ztesoft.iocp.utils.HttpUtils;
import com.ztesoft.iocp.utils.JsonUtils;

/**
 * <Description>自动生成代码 <br>
 * 
 * @author lifei<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年12月11日 <br>
 * @since V1.0<br>
 */

@Controller
@RequestMapping("/base")
public class BaseController {

    @RequestMapping("updatePrivilegeItems")
    @ResponseBody
    public Map<String,Object> updatePrivilegeItems(HttpServletRequest request,String tableName,String entityStr){
        String url=Configuration.getString("baseURL")+"/login?type=updatePrivilegeItems";
        Map<String,Object> result = new HashMap<String, Object>();
        try{
            Map<String, Object> map=JsonUtils.toMap(HttpUtils.getDataByUrl(url, "tableName="+tableName+"&entityStr="+encode(entityStr)));
            result.put("success", map.get("success"));
        }catch(Exception e){
            e.printStackTrace();
            result.put("success", false);
            result.put("message", e.getMessage()+e.toString());
        }
        return result;
    }
    @RequestMapping("getPrivilegeList")
    @ResponseBody
    public Map<String,Object> getPrivilegeList(HttpServletRequest request,String tableName,String conditionsKey){
        String url=Configuration.getString("baseURL")+"/login?type=getPrivilegeList";
        Map<String,Object> result = new HashMap<String, Object>();
        try{
            Map<String, Object> map=JsonUtils.toMap(HttpUtils.getDataByUrl(url, "tableName="+tableName+"&conditionsKey="+conditionsKey));
            return map;
        }catch(Exception e){
            e.printStackTrace();
            result.put("success", false);
            result.put("message", e.getMessage()+e.toString());
        }
        return result;
    }
    
    private String encode(String s){
        StringBuffer output = new StringBuffer();
        for (int i = 0; i < s.length(); i++){
            char a = s.charAt(i);
            if(isChinese(a)){
                output.append("\\u" +Integer.toString(a, 16));
            }else{
                output.append(a);
            }
        }      
        return output.toString();
    }
    
    private  boolean isChinese(char c) {  
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);  
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS  
  
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS  
  
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A  
  
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION  
  
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION  
  
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {  
  
            return true;  
        }  
        return false;  
    }  
    
}
